﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1.Templates
{
    public class DapperBLLTemplate : TemplatesBase
    {
        public DapperBLLTemplate() : base("Bll")
        {
        }

        /// <summary>
        /// 命名空间
        /// </summary>
        public override string namespacestr
        {
            get
            {
                return Entrance.attrnamespacestr + ".BLL.Impl";
            }
        }

        public override void SaveFile(List<EntityAttrList> list)
        {
            if (list != null && list.Count > 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                //表描述
                string txtTableDesc = list.FirstOrDefault().TableDescription;
                string txtTableName = list.FirstOrDefault().TableName;
                stringBuilder.AppendLine("using DapperDal.DapperBase;");
                stringBuilder.AppendLine("using " + Entrance.attrnamespacestr + ".DAL; ");
                stringBuilder.AppendLine("using " + Entrance.attrnamespacestr + ".DAL.Impl; ");
                stringBuilder.AppendLine("using " + Entrance.attrnamespacestr + ".Entity; ");
                stringBuilder.AppendLine("");
                stringBuilder.AppendLine("namespace " + namespacestr);
                stringBuilder.AppendLine("{");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// <summary>");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// " + txtTableDesc + "业务层");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// <summary>");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("public partial class " + txtTableName + FileExt + " : BaseBll<" + GetPrimaryKey(list) + "," + txtTableName + "Entity>, I" + txtTableName + "Bll");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("{");

                //s.Substring(0, 1).ToUpper() + s.Substring(1);

                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("I" + txtTableName + "Dal _" + txtTableName.Substring(0, 1).ToLower() + txtTableName.Substring(1) + "Dal;");
                stringBuilder.AppendLine("");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("#region 构造函数");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("public " + txtTableName + FileExt + "()");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("{");
                stringBuilder.Append(new string(' ', 12));
                stringBuilder.AppendLine("_" + txtTableName.Substring(0, 1).ToLower() + txtTableName.Substring(1) + "Dal = new " + txtTableName + "Dal();");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("}");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("#endregion");

                stringBuilder.AppendLine("");


                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("}");
                stringBuilder.AppendLine("}");

                CreateFile(txtTableName, stringBuilder.ToString());
            }
        }
    }
}
